/**
 * 
 */
package com.main.springjdbc.springconnect;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;

/**
 * @author Administrator
 *
 */
public class JdbcTest {
       private ApplicationContext c = null;
       private JdbcTemplate template;
       {
    	   c = new ClassPathXmlApplicationContext("com/org/javabody/springjdbc/SpringJdbc-config.xml");
    	   template = (JdbcTemplate)c.getBean("jdbcTemplate");
       }

       public void setDataSource() throws SQLException {
    	   DataSource ds = c.getBean(DataSource.class);
    	   System.out.println(ds.getConnection());
       }
       
//       public void query() {
//    	   List result = template.query("select my_number from t_springjdbc_test",
//            new SingleColumnRowMapper());
//    	   System.out.println(result);
//       }


//       


       public Long getSerlizes() {  
           String sql="select sq_aaz330.nextval from dual";  
           //可以通过NamedParameterJdbcTemplate得到JdbcTemplate  
           Long ser = template.queryForObject(sql, Long.class);
           return ser;
        }  
       
       /**
        * 多条数据查询
        */
       public void batchQuery() {
    	   String sql = " select aaz330 from qyd2 where aaz330 > ?";
    	   RowMapper<List> row = new BeanPropertyRowMapper<>(List.class);
    	   List list = template.query(sql, row,5);
    	   System.out.println(list);
       }
          
       /**
        * 批量插入
        */
       public void batchInsert() {  
           String sql="insert into  qyd2(aaz330, aae001, aac001,aaz001,aab001) "
           		+ " values (?,?,?,?,?) ";  
           List<Object[]> list = new ArrayList<Object[]>();
           list.add(new Object[]{this.getSerlizes(),"2016","12","1810","1810"});
           list.add(new Object[]{this.getSerlizes(),"2016","13","1810","1810"});
           list.add(new Object[]{this.getSerlizes(),"2016","14","1810","1810"});
           list.add(new Object[]{this.getSerlizes(),"2016","15","1810","1810"});
           template.batchUpdate(sql, list);
           //System.out.println(count);
        }  
       
       
	/**
	 * @param args
	 */
	public static void main(String[] args) {
            try {
				new JdbcTest().setDataSource();
				//new JdbcTest().batchInsert();
				new JdbcTest().batchQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}


}
